package org.ucas.faker.web.aop;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.ucas.faker.web.utils.model.Context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public class DepotSwitcher implements HandlerInterceptor {
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    Map paras = request.getParameterMap();
    if(paras.containsKey("depot_id")) {
      String[] value = (String[]) paras.get("depot_id");
      if(value.length == 1) {
        Context ctx = (Context) request.getSession().getAttribute("context");
        if(ctx == null) {
          ctx = new Context();
          request.getSession().setAttribute("context", ctx);
        }
        ctx.depotId = Long.parseLong(value[0]);
      }
    }
    return true;
  }

  @Override
  public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

  }

  @Override
  public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

  }
}
